<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>dom.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_dom.js.html">dom.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2008 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Testing utilities for DOM related tests.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.testing.dom&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.dom&#39;);
<a name="line23"></a>goog.require(&#39;goog.dom.NodeIterator&#39;);
<a name="line24"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom.TagIterator&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line27"></a>goog.require(&#39;goog.dom.classes&#39;);
<a name="line28"></a>goog.require(&#39;goog.iter&#39;);
<a name="line29"></a>goog.require(&#39;goog.object&#39;);
<a name="line30"></a>goog.require(&#39;goog.string&#39;);
<a name="line31"></a>goog.require(&#39;goog.style&#39;);
<a name="line32"></a>goog.require(&#39;goog.testing.asserts&#39;);
<a name="line33"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>/**
<a name="line37"></a> * A unique object to use as an end tag marker.
<a name="line38"></a> * @type {Object}
<a name="line39"></a> * @private
<a name="line40"></a> */
<a name="line41"></a>goog.testing.dom.END_TAG_MARKER_ = {};
<a name="line42"></a>
<a name="line43"></a>
<a name="line44"></a>/**
<a name="line45"></a> * Tests if the given iterator over nodes matches the given Array of node
<a name="line46"></a> * descriptors.  Throws an error if any match fails.
<a name="line47"></a> * @param {goog.iter.Iterator} it  An iterator over nodes.
<a name="line48"></a> * @param {Array.&lt;Node|number|string&gt;} array Array of node descriptors to match
<a name="line49"></a> *     against.  Node descriptors can be any of the following:
<a name="line50"></a> *         Node: Test if the two nodes are equal.
<a name="line51"></a> *         number: Test node.nodeType == number.
<a name="line52"></a> *         string starting with &#39;#&#39;: Match the node&#39;s id with the text
<a name="line53"></a> *             after &quot;#&quot;.
<a name="line54"></a> *         other string: Match the text node&#39;s contents.
<a name="line55"></a> *
<a name="line56"></a> */
<a name="line57"></a>goog.testing.dom.assertNodesMatch = function(it, array) {
<a name="line58"></a>  var i = 0;
<a name="line59"></a>  goog.iter.forEach(it, function(node) {
<a name="line60"></a>    if (array.length &lt;= i) {
<a name="line61"></a>      fail(&#39;Got more nodes than expected: &#39; + goog.testing.dom.describeNode_(
<a name="line62"></a>          node));
<a name="line63"></a>    }
<a name="line64"></a>    var expected = array[i];
<a name="line65"></a>
<a name="line66"></a>    if (goog.dom.isNodeLike(expected)) {
<a name="line67"></a>      assertEquals(&#39;Nodes should match at position &#39; + i, expected, node);
<a name="line68"></a>    } else if (goog.isNumber(expected)) {
<a name="line69"></a>      assertEquals(&#39;Node types should match at position &#39; + i, expected,
<a name="line70"></a>        node.nodeType);
<a name="line71"></a>    } else if (expected.charAt(0) == &#39;#&#39;) {
<a name="line72"></a>      assertEquals(&#39;Expected element at position &#39; + i,
<a name="line73"></a>          goog.dom.NodeType.ELEMENT, node.nodeType);
<a name="line74"></a>      var expectedId = expected.substr(1);
<a name="line75"></a>      assertEquals(&#39;IDs should match at position &#39; + i,
<a name="line76"></a>          expectedId, node.id);
<a name="line77"></a>
<a name="line78"></a>    } else {
<a name="line79"></a>      assertEquals(&#39;Expected text node at position &#39; + i,
<a name="line80"></a>          goog.dom.NodeType.TEXT, node.nodeType);
<a name="line81"></a>      assertEquals(&#39;Node contents should match at position &#39; + i,
<a name="line82"></a>          expected, node.nodeValue);
<a name="line83"></a>    }
<a name="line84"></a>
<a name="line85"></a>    i++;
<a name="line86"></a>  });
<a name="line87"></a>
<a name="line88"></a>  assertEquals(&#39;Used entire match array&#39;, array.length, i);
<a name="line89"></a>};
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>/**
<a name="line93"></a> * Determines if the current user agent matches the specified string.  Returns
<a name="line94"></a> * false if the string does specify at least one user agent but does not match
<a name="line95"></a> * the running agent.
<a name="line96"></a> * @param {string} userAgents Space delimited string of user agents.
<a name="line97"></a> * @return {boolean} Whether the user agent was matched.  Also true if no user
<a name="line98"></a> *     agent was listed in the expectation string.
<a name="line99"></a> * @private
<a name="line100"></a> */
<a name="line101"></a>goog.testing.dom.checkUserAgents_ = function(userAgents) {
<a name="line102"></a>  if (goog.string.startsWith(userAgents, &#39;!&#39;)) {
<a name="line103"></a>    if (goog.string.contains(userAgents, &#39; &#39;)) {
<a name="line104"></a>      throw new Error(&#39;Only a single negative user agent may be specified&#39;);
<a name="line105"></a>    }
<a name="line106"></a>    return !goog.userAgent[userAgents.substr(1)];
<a name="line107"></a>  }
<a name="line108"></a>
<a name="line109"></a>  var agents = userAgents.split(&#39; &#39;);
<a name="line110"></a>  var hasUserAgent = false;
<a name="line111"></a>  for (var i = 0, len = agents.length; i &lt; len; i++) {
<a name="line112"></a>    var cls = agents[i];
<a name="line113"></a>    if (cls in goog.userAgent) {
<a name="line114"></a>      hasUserAgent = true;
<a name="line115"></a>      if (goog.userAgent[cls]) {
<a name="line116"></a>        return true;
<a name="line117"></a>      }
<a name="line118"></a>    }
<a name="line119"></a>  }
<a name="line120"></a>  // If we got here, there was a user agent listed but we didn&#39;t match it.
<a name="line121"></a>  return !hasUserAgent;
<a name="line122"></a>};
<a name="line123"></a>
<a name="line124"></a>
<a name="line125"></a>/**
<a name="line126"></a> * Map function that converts end tags to a specific object.
<a name="line127"></a> * @param {Node} node The node to map.
<a name="line128"></a> * @param {Object} ignore Always undefined.
<a name="line129"></a> * @param {goog.dom.TagIterator} iterator The iterator.
<a name="line130"></a> * @return {Node|Object} The resulting iteration item.
<a name="line131"></a> * @private
<a name="line132"></a> */
<a name="line133"></a>goog.testing.dom.endTagMap_ = function(node, ignore, iterator) {
<a name="line134"></a>  return iterator.isEndTag() ? goog.testing.dom.END_TAG_MARKER_ : node;
<a name="line135"></a>};
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/**
<a name="line139"></a> * Check if the given node is important.  A node is important if it is a
<a name="line140"></a> * non-empty text node, a non-annotated element, or an element annotated to
<a name="line141"></a> * match on this user agent.
<a name="line142"></a> * @param {Node} node The node to test.
<a name="line143"></a> * @return {boolean} Whether this node should be included for iteration.
<a name="line144"></a> * @private
<a name="line145"></a> */
<a name="line146"></a>goog.testing.dom.nodeFilter_ = function(node) {
<a name="line147"></a>  if (node.nodeType == goog.dom.NodeType.TEXT) {
<a name="line148"></a>    // If a node is part of a string of text nodes and it has spaces in it,
<a name="line149"></a>    // we allow it since it&#39;s going to affect the merging of nodes done below.
<a name="line150"></a>    if (goog.string.isBreakingWhitespace(node.nodeValue) &amp;&amp;
<a name="line151"></a>        (!node.previousSibling ||
<a name="line152"></a>             node.previousSibling.nodeType != goog.dom.NodeType.TEXT) &amp;&amp;
<a name="line153"></a>        (!node.nextSibling ||
<a name="line154"></a>             node.nextSibling.nodeType != goog.dom.NodeType.TEXT)) {
<a name="line155"></a>      return false;
<a name="line156"></a>    }
<a name="line157"></a>    // Allow optional text to be specified as [[BROWSER1 BROWSER2]]Text
<a name="line158"></a>    var match = node.nodeValue.match(/^\[\[(.+)\]\]/);
<a name="line159"></a>    if (match) {
<a name="line160"></a>      return goog.testing.dom.checkUserAgents_(match[1]);
<a name="line161"></a>    }
<a name="line162"></a>  } else if (node.className) {
<a name="line163"></a>    return goog.testing.dom.checkUserAgents_(node.className);
<a name="line164"></a>  }
<a name="line165"></a>  return true;
<a name="line166"></a>};
<a name="line167"></a>
<a name="line168"></a>
<a name="line169"></a>/**
<a name="line170"></a> * Determines the text to match from the given node, removing browser
<a name="line171"></a> * specification strings.
<a name="line172"></a> * @param {Node} node The node expected to match.
<a name="line173"></a> * @return {string} The text, stripped of browser specification strings.
<a name="line174"></a> * @private
<a name="line175"></a> */
<a name="line176"></a>goog.testing.dom.getExpectedText_ = function(node) {
<a name="line177"></a>  // Strip off the browser specifications.
<a name="line178"></a>  /^(\[\[.+\]\])?(.*)/.test(node.nodeValue);
<a name="line179"></a>  return RegExp.$2;
<a name="line180"></a>};
<a name="line181"></a>
<a name="line182"></a>/**
<a name="line183"></a> * Describes the given node.
<a name="line184"></a> * @param {Node} node The node to describe.
<a name="line185"></a> * @return {string} A description of the node.
<a name="line186"></a> * @private
<a name="line187"></a> */
<a name="line188"></a>goog.testing.dom.describeNode_ = function(node) {
<a name="line189"></a>  if (node.nodeType == goog.dom.NodeType.TEXT) {
<a name="line190"></a>    return &#39;[Text: &#39; + node.nodeValue + &#39;]&#39;;
<a name="line191"></a>  } else {
<a name="line192"></a>    return &#39;&lt;&#39; + node.tagName + (node.id ? &#39; #&#39; + node.id : &#39;&#39;) + &#39; .../&gt;&#39;;
<a name="line193"></a>  }
<a name="line194"></a>};
<a name="line195"></a>
<a name="line196"></a>
<a name="line197"></a>/**
<a name="line198"></a> * Assert that the html in {@code actual} is substantially similar to
<a name="line199"></a> * htmlPattern.  This method tests for the same set of styles, for the same
<a name="line200"></a> * order of nodes, and the presence of attributes.  Breaking whitespace nodes
<a name="line201"></a> * are ignored.  Elements can be
<a name="line202"></a> * annotated with classnames corresponding to keys in goog.userAgent and will be
<a name="line203"></a> * expected to show up in that user agent and expected not to show up in
<a name="line204"></a> * others.
<a name="line205"></a> * @param {string} htmlPattern The pattern to match.
<a name="line206"></a> * @param {Element} actual The element to check: its contents are matched
<a name="line207"></a> *     against the HTML pattern.
<a name="line208"></a> * @param {boolean} opt_strictAttributes If false, attributes that appear in
<a name="line209"></a> *     htmlPattern must be in actual, but actual can have attributes not
<a name="line210"></a> *     present in htmlPattern.  If true, htmlPattern and actual must have the
<a name="line211"></a> *     same set of attributes.  Default is false.
<a name="line212"></a> */
<a name="line213"></a>goog.testing.dom.assertHtmlContentsMatch = function(htmlPattern, actual,
<a name="line214"></a>    opt_strictAttributes) {
<a name="line215"></a>  var div = goog.dom.createDom(goog.dom.TagName.DIV);
<a name="line216"></a>  div.innerHTML = htmlPattern;
<a name="line217"></a>
<a name="line218"></a>  var errorSuffix = &#39;\nExpected\n&#39; + htmlPattern + &#39;\nActual\n&#39; +
<a name="line219"></a>      actual.innerHTML;
<a name="line220"></a>
<a name="line221"></a>  var actualIt = goog.iter.filter(
<a name="line222"></a>      goog.iter.map(new goog.dom.TagIterator(actual),
<a name="line223"></a>          goog.testing.dom.endTagMap_),
<a name="line224"></a>      goog.testing.dom.nodeFilter_);
<a name="line225"></a>
<a name="line226"></a>  var expectedIt = goog.iter.filter(new goog.dom.NodeIterator(div),
<a name="line227"></a>      goog.testing.dom.nodeFilter_);
<a name="line228"></a>  var actualNode;
<a name="line229"></a>
<a name="line230"></a>  var preIterated = false;
<a name="line231"></a>  var advanceActualNode = function() {
<a name="line232"></a>    // If the iterator has already been advanced, don&#39;t advance it again.
<a name="line233"></a>    if (!preIterated) {
<a name="line234"></a>      actualNode = /** @type {Node} */ (goog.iter.nextOrValue(actualIt, null));
<a name="line235"></a>    }
<a name="line236"></a>    preIterated = false;
<a name="line237"></a>
<a name="line238"></a>    // Advance the iterator so long as it is return end tags.
<a name="line239"></a>    while (actualNode == goog.testing.dom.END_TAG_MARKER_) {
<a name="line240"></a>      actualNode = /** @type {Node} */ (goog.iter.nextOrValue(actualIt, null));
<a name="line241"></a>    }
<a name="line242"></a>  };
<a name="line243"></a>
<a name="line244"></a>  var number = 0;
<a name="line245"></a>  goog.iter.forEach(expectedIt, function(expectedNode) {
<a name="line246"></a>    advanceActualNode();
<a name="line247"></a>    assertNotNull(&#39;Finished actual HTML before finishing expected HTML at &#39; +
<a name="line248"></a>                  &#39;node number &#39; + number + &#39;: &#39; +
<a name="line249"></a>                  goog.testing.dom.describeNode_(expectedNode) + errorSuffix,
<a name="line250"></a>                  actualNode);
<a name="line251"></a>
<a name="line252"></a>    // Do no processing for expectedNode == div.
<a name="line253"></a>    if (expectedNode == div) {
<a name="line254"></a>      return;
<a name="line255"></a>    }
<a name="line256"></a>
<a name="line257"></a>    assertEquals(&#39;Should have the same node type, got &#39; +
<a name="line258"></a>        goog.testing.dom.describeNode_(actualNode) + &#39; but expected &#39; +
<a name="line259"></a>        goog.testing.dom.describeNode_(expectedNode) + &#39;.&#39; + errorSuffix,
<a name="line260"></a>        expectedNode.nodeType, actualNode.nodeType);
<a name="line261"></a>
<a name="line262"></a>    if (expectedNode.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line263"></a>      assertEquals(&#39;Tag names should match&#39; + errorSuffix,
<a name="line264"></a>          expectedNode.tagName, actualNode.tagName);
<a name="line265"></a>      assertObjectEquals(&#39;Should have same styles&#39; + errorSuffix,
<a name="line266"></a>          goog.style.parseStyleAttribute(expectedNode.style.cssText),
<a name="line267"></a>          goog.style.parseStyleAttribute(actualNode.style.cssText));
<a name="line268"></a>      goog.testing.dom.assertAttributesEqual_(errorSuffix, expectedNode,
<a name="line269"></a>          actualNode, !!opt_strictAttributes);
<a name="line270"></a>    } else {
<a name="line271"></a>      // Concatenate text nodes until we reach a non text node.
<a name="line272"></a>      var actualText = actualNode.nodeValue;
<a name="line273"></a>      preIterated = true;
<a name="line274"></a>      while ((actualNode = goog.iter.nextOrValue(actualIt, null)) &amp;&amp;
<a name="line275"></a>          actualNode.nodeType == goog.dom.NodeType.TEXT) {
<a name="line276"></a>        actualText += actualNode.nodeValue;
<a name="line277"></a>      }
<a name="line278"></a>
<a name="line279"></a>      var expectedText = goog.testing.dom.getExpectedText_(expectedNode);
<a name="line280"></a>      if ((actualText &amp;&amp; !goog.string.isBreakingWhitespace(actualText)) ||
<a name="line281"></a>          (expectedText &amp;&amp; !goog.string.isBreakingWhitespace(expectedText))) {
<a name="line282"></a>        var normalizedActual = actualText.replace(/\s+/g, &#39; &#39;);
<a name="line283"></a>        var normalizedExpected = expectedText.replace(/\s+/g, &#39; &#39;);
<a name="line284"></a>
<a name="line285"></a>        assertEquals(&#39;Text should match&#39; + errorSuffix, normalizedExpected,
<a name="line286"></a>            normalizedActual);
<a name="line287"></a>      }
<a name="line288"></a>    }
<a name="line289"></a>
<a name="line290"></a>    number++;
<a name="line291"></a>  });
<a name="line292"></a>
<a name="line293"></a>  advanceActualNode();
<a name="line294"></a>  assertNull(&#39;Finished expected HTML before finishing actual HTML&#39; +
<a name="line295"></a>      errorSuffix, goog.iter.nextOrValue(actualIt, null));
<a name="line296"></a>};
<a name="line297"></a>
<a name="line298"></a>
<a name="line299"></a>/**
<a name="line300"></a> * Assert that the html in {@code actual} is substantially similar to
<a name="line301"></a> * htmlPattern.  This method tests for the same set of styles, and for the same
<a name="line302"></a> * order of nodes.  Breaking whitespace nodes are ignored.  Elements can be
<a name="line303"></a> * annotated with classnames corresponding to keys in goog.userAgent and will be
<a name="line304"></a> * expected to show up in that user agent and expected not to show up in
<a name="line305"></a> * others.
<a name="line306"></a> * @param {string} htmlPattern The pattern to match.
<a name="line307"></a> * @param {string} actual The html to check.
<a name="line308"></a> */
<a name="line309"></a>goog.testing.dom.assertHtmlMatches = function(htmlPattern, actual) {
<a name="line310"></a>  var div = goog.dom.createDom(goog.dom.TagName.DIV);
<a name="line311"></a>  div.innerHTML = actual;
<a name="line312"></a>
<a name="line313"></a>  goog.testing.dom.assertHtmlContentsMatch(htmlPattern, div);
<a name="line314"></a>};
<a name="line315"></a>
<a name="line316"></a>
<a name="line317"></a>/**
<a name="line318"></a> * Finds the first text node descendant of root with the given content.  Note
<a name="line319"></a> * that this operates on a text node level, so if text nodes get split this
<a name="line320"></a> * may not match the user visible text.  Using normalize() may help here.
<a name="line321"></a> * @param {string|RegExp} textOrRegexp The text to find, or a regular
<a name="line322"></a> *     expression to find a match of.
<a name="line323"></a> * @param {Element} root The element to search in.
<a name="line324"></a> * @return {Node} The first text node that matches, or null if none is found.
<a name="line325"></a> */
<a name="line326"></a>goog.testing.dom.findTextNode = function(textOrRegexp, root) {
<a name="line327"></a>  var it = new goog.dom.NodeIterator(root);
<a name="line328"></a>  var ret = goog.iter.nextOrValue(goog.iter.filter(it, function(node) {
<a name="line329"></a>    if (node.nodeType == goog.dom.NodeType.TEXT) {
<a name="line330"></a>      if (goog.isString(textOrRegexp)) {
<a name="line331"></a>        return node.nodeValue == textOrRegexp;
<a name="line332"></a>      } else {
<a name="line333"></a>        return !!node.nodeValue.match(textOrRegexp);
<a name="line334"></a>      }
<a name="line335"></a>    } else {
<a name="line336"></a>      return false;
<a name="line337"></a>    }
<a name="line338"></a>  }), null);
<a name="line339"></a>  return /** @type {Node} */ (ret);
<a name="line340"></a>};
<a name="line341"></a>
<a name="line342"></a>
<a name="line343"></a>/**
<a name="line344"></a> * Assert the end points of a range.
<a name="line345"></a> *
<a name="line346"></a> * Notice that &quot;Are two ranges visually identical?&quot; and &quot;Do two ranges have
<a name="line347"></a> * the same endpoint?&quot; are independent questions. Two visually identical ranges
<a name="line348"></a> * may have different endpoints. And two ranges with the same endpoints may
<a name="line349"></a> * be visually different.
<a name="line350"></a> *
<a name="line351"></a> * @param {Node} start The expected start node.
<a name="line352"></a> * @param {number} startOffset The expected start offset.
<a name="line353"></a> * @param {Node} end The expected end node.
<a name="line354"></a> * @param {number} endOffset The expected end offset.
<a name="line355"></a> * @param {goog.dom.AbstractRange} range The actual range.
<a name="line356"></a> */
<a name="line357"></a>goog.testing.dom.assertRangeEquals = function(start, startOffset, end,
<a name="line358"></a>    endOffset, range) {
<a name="line359"></a>  assertEquals(&#39;Unexpected start node&#39;, start, range.getStartNode());
<a name="line360"></a>  assertEquals(&#39;Unexpected end node&#39;, end, range.getEndNode());
<a name="line361"></a>  assertEquals(&#39;Unexpected start offset&#39;, startOffset, range.getStartOffset());
<a name="line362"></a>  assertEquals(&#39;Unexpected end offset&#39;, endOffset, range.getEndOffset());
<a name="line363"></a>};
<a name="line364"></a>
<a name="line365"></a>
<a name="line366"></a>/**
<a name="line367"></a> * Gets the value of a DOM attribute in deterministic way.
<a name="line368"></a> * @param {!Node} node A node.
<a name="line369"></a> * @param {string} name Attribute name.
<a name="line370"></a> * @return {*} Attribute value.
<a name="line371"></a> * @private
<a name="line372"></a> */
<a name="line373"></a>goog.testing.dom.getAttributeValue_ = function(node, name) {
<a name="line374"></a>  // These hacks avoid nondetermistic results in the following cases:
<a name="line375"></a>  // IE7: document.createElement(&#39;input&#39;).height returns a random number.
<a name="line376"></a>  // FF3: getAttribute(&#39;disabled&#39;) returns different value for &lt;div disabled=&quot;&quot;&gt;
<a name="line377"></a>  //      and &lt;div disabled=&quot;disabled&quot;&gt;
<a name="line378"></a>  // WebKit: Two radio buttons with the same name can&#39;t be checked at the same
<a name="line379"></a>  //      time, even if only one of them is in the document.
<a name="line380"></a>  if (goog.userAgent.WEBKIT &amp;&amp; node.tagName == &#39;INPUT&#39; &amp;&amp;
<a name="line381"></a>      node[&#39;type&#39;] == &#39;radio&#39; &amp;&amp; name == &#39;checked&#39;) {
<a name="line382"></a>    return false;
<a name="line383"></a>  }
<a name="line384"></a>  return goog.isDef(node[name]) &amp;&amp;
<a name="line385"></a>      typeof node.getAttribute(name) != typeof node[name] ?
<a name="line386"></a>      node[name] : node.getAttribute(name);
<a name="line387"></a>};
<a name="line388"></a>
<a name="line389"></a>
<a name="line390"></a>/**
<a name="line391"></a> * Assert that the attributes of two Nodes are the same (ignoring any
<a name="line392"></a> * instances of the style attribute).
<a name="line393"></a> * @param {string} errorSuffix String to add to end of error messages.
<a name="line394"></a> * @param {Node} expectedNode The node whose attributes we are expecting.
<a name="line395"></a> * @param {Node} actualNode The node with the actual attributes.
<a name="line396"></a> * @param {boolean} strictAttributes If false, attributes that appear in
<a name="line397"></a> *     expectedNode must also be in actualNode, but actualNode can have
<a name="line398"></a> *     attributes not present in expectedNode.  If true, expectedNode and
<a name="line399"></a> *     actualNode must have the same set of attributes.
<a name="line400"></a> * @private
<a name="line401"></a> */
<a name="line402"></a>goog.testing.dom.assertAttributesEqual_ = function(errorSuffix,
<a name="line403"></a>    expectedNode, actualNode, strictAttributes) {
<a name="line404"></a>  if (strictAttributes) {
<a name="line405"></a>    goog.testing.dom.compareClassAttribute_(expectedNode, actualNode);
<a name="line406"></a>  }
<a name="line407"></a>
<a name="line408"></a>  var expectedAttributes = expectedNode.attributes;
<a name="line409"></a>  var actualAttributes = actualNode.attributes;
<a name="line410"></a>
<a name="line411"></a>  for (var i = 0, len = expectedAttributes.length; i &lt; len; i++) {
<a name="line412"></a>    var expectedName = expectedAttributes[i].name;
<a name="line413"></a>    var expectedValue = goog.testing.dom.getAttributeValue_(expectedNode,
<a name="line414"></a>        expectedName);
<a name="line415"></a>
<a name="line416"></a>    var actualAttribute = actualAttributes[expectedName];
<a name="line417"></a>
<a name="line418"></a>    if (expectedName == &#39;id&#39; &amp;&amp; goog.userAgent.IE) {
<a name="line419"></a>      goog.testing.dom.compareIdAttributeForIe_(
<a name="line420"></a>          /** @type {string} */ (expectedValue), actualAttribute,
<a name="line421"></a>          strictAttributes, errorSuffix);
<a name="line422"></a>      continue;
<a name="line423"></a>    }
<a name="line424"></a>
<a name="line425"></a>    if (goog.testing.dom.ignoreAttribute_(expectedName)) {
<a name="line426"></a>      continue;
<a name="line427"></a>    }
<a name="line428"></a>
<a name="line429"></a>    assertNotUndefined(&#39;Expected to find attribute with name &#39; +
<a name="line430"></a>        expectedName + &#39;, in element &#39; +
<a name="line431"></a>        goog.testing.dom.describeNode_(actualNode) + errorSuffix,
<a name="line432"></a>        actualAttribute);
<a name="line433"></a>    assertEquals(&#39;Expected attribute &#39; + expectedName +
<a name="line434"></a>        &#39; has a different value &#39; + errorSuffix,
<a name="line435"></a>        expectedValue,
<a name="line436"></a>        goog.testing.dom.getAttributeValue_(actualNode, actualAttribute.name));
<a name="line437"></a>  }
<a name="line438"></a>
<a name="line439"></a>  if (strictAttributes) {
<a name="line440"></a>    for (i = 0; i &lt; actualAttributes.length; i++) {
<a name="line441"></a>      var actualName = actualAttributes[i].name;
<a name="line442"></a>
<a name="line443"></a>      if (goog.testing.dom.ignoreAttribute_(actualName)) {
<a name="line444"></a>        continue;
<a name="line445"></a>      }
<a name="line446"></a>
<a name="line447"></a>      assertNotUndefined(&#39;Unexpected attribute with name &#39; +
<a name="line448"></a>          actualName + &#39; in element &#39; +
<a name="line449"></a>          goog.testing.dom.describeNode_(actualNode) + errorSuffix,
<a name="line450"></a>          expectedAttributes[actualName]);
<a name="line451"></a>    }
<a name="line452"></a>  }
<a name="line453"></a>};
<a name="line454"></a>
<a name="line455"></a>
<a name="line456"></a>/**
<a name="line457"></a> * Assert the class attribute of actualNode is the same as the one in
<a name="line458"></a> * expectedNode, ignoring classes that are useragents.
<a name="line459"></a> * @param {Node} expectedNode The DOM node whose class we expect.
<a name="line460"></a> * @param {Node} actualNode The DOM node with the actual class.
<a name="line461"></a> * @private
<a name="line462"></a> */
<a name="line463"></a>goog.testing.dom.compareClassAttribute_ = function(expectedNode,
<a name="line464"></a>    actualNode) {
<a name="line465"></a>  var classes = goog.dom.classes.get(expectedNode);
<a name="line466"></a>
<a name="line467"></a>  var expectedClasses = [];
<a name="line468"></a>  for (var i = 0, len = classes.length; i &lt; len; i++) {
<a name="line469"></a>    if (!(classes[i] in goog.userAgent)) {
<a name="line470"></a>      expectedClasses.push(classes[i]);
<a name="line471"></a>    }
<a name="line472"></a>  }
<a name="line473"></a>  expectedClasses.sort();
<a name="line474"></a>
<a name="line475"></a>  var actualClasses = goog.dom.classes.get(actualNode);
<a name="line476"></a>  actualClasses.sort();
<a name="line477"></a>
<a name="line478"></a>  assertArrayEquals(
<a name="line479"></a>      &#39;Expected class was: &#39; + expectedClasses.join(&#39; &#39;) +
<a name="line480"></a>      &#39;, but actual class was: &#39; + actualNode.className,
<a name="line481"></a>      expectedClasses, actualClasses);
<a name="line482"></a>};
<a name="line483"></a>
<a name="line484"></a>
<a name="line485"></a>/**
<a name="line486"></a> * Set of attributes IE adds to elements randomly.
<a name="line487"></a> * @type {Object}
<a name="line488"></a> * @private
<a name="line489"></a> */
<a name="line490"></a>goog.testing.dom.BAD_IE_ATTRIBUTES_ = goog.object.createSet(
<a name="line491"></a>    &#39;methods&#39;, &#39;CHECKED&#39;, &#39;dataFld&#39;, &#39;dataFormatAs&#39;, &#39;dataSrc&#39;);
<a name="line492"></a>
<a name="line493"></a>
<a name="line494"></a>/**
<a name="line495"></a> * Whether to ignore the attribute.
<a name="line496"></a> * @param {string} name Name of the attribute.
<a name="line497"></a> * @return {boolean} True if the attribute should be ignored.
<a name="line498"></a> * @private
<a name="line499"></a> */
<a name="line500"></a>goog.testing.dom.ignoreAttribute_ = function(name) {
<a name="line501"></a>  if (name == &#39;style&#39; || name == &#39;class&#39;) {
<a name="line502"></a>    return true;
<a name="line503"></a>  }
<a name="line504"></a>  return goog.userAgent.IE &amp;&amp; goog.testing.dom.BAD_IE_ATTRIBUTES_[name];
<a name="line505"></a>};
<a name="line506"></a>
<a name="line507"></a>
<a name="line508"></a>/**
<a name="line509"></a> * Compare id attributes for IE.  In IE, if an element lacks an id attribute
<a name="line510"></a> * in the original HTML, the element object will still have such an attribute,
<a name="line511"></a> * but its value will be the empty string.
<a name="line512"></a> * @param {string} expectedValue The expected value of the id attribute.
<a name="line513"></a> * @param {Attr} actualAttribute The actual id attribute.
<a name="line514"></a> * @param {boolean} strictAttributes Whether strict attribute checking should be
<a name="line515"></a> *     done.
<a name="line516"></a> * @param {string} errorSuffix String to append to error messages.
<a name="line517"></a> * @private
<a name="line518"></a> */
<a name="line519"></a>goog.testing.dom.compareIdAttributeForIe_ = function(expectedValue,
<a name="line520"></a>    actualAttribute, strictAttributes, errorSuffix) {
<a name="line521"></a>  if (expectedValue === &#39;&#39;) {
<a name="line522"></a>    if (strictAttributes) {
<a name="line523"></a>      assertTrue(&#39;Unexpected attribute with name id in element &#39; +
<a name="line524"></a>          errorSuffix, actualAttribute.value == &#39;&#39;);
<a name="line525"></a>    }
<a name="line526"></a>  } else {
<a name="line527"></a>    assertNotUndefined(&#39;Expected to find attribute with name id, in element &#39; +
<a name="line528"></a>        errorSuffix, actualAttribute);
<a name="line529"></a>    assertNotEquals(&#39;Expected to find attribute with name id, in element &#39; +
<a name="line530"></a>        errorSuffix, &#39;&#39;, actualAttribute.value);
<a name="line531"></a>    assertEquals(&#39;Expected attribute has a different value &#39; + errorSuffix,
<a name="line532"></a>        expectedValue, actualAttribute.value);
<a name="line533"></a>  }
<a name="line534"></a>};
</pre>


</body>
</html>
